/**
 * create by ck on 2017/7/1.
 */
$(document).ready(function(){
    var $dataGrid   = new xhDataGrid($('#List'), 'Appserver/getFirewall'),
        $btSave     = $('#bt_form_add'),
        $form       = new xhForm($('#xh_form'));
    $dataGrid.parseToolbar().init().activeButton([], ['bt_list_del', 'bt_list_enable', 'bt_list_disable']);
    $form.parse().bindRequired($btSave);
    xhExtra.bindDefaultFun($dataGrid, $form, $btSave, {
        modifyUrl: 'Appserver/createFirewall',
        deleteUrl: 'Appserver/delFirewall',
        idField: 'firewallAppId'
    });
    //双击打开修改网络列表
    $dataGrid.onDblClickRow = function(rowIndex, rowData){
        window.top.swal({
            titleText: rowData.name || rowData.packageName,
            input: 'select',
            inputOptions: {
                1: '3GWIFI',
                2: 'WIFI',
                3: '3G',
                4: '禁用'
            },
            inputPlaceholder: '选择网络',
            inputAttributes: {class: 'form-control'},
            showCancelButton: true,
            cancelButtonText: '取消',
            confirmButtonText: '确认',
            showLoaderOnConfirm: true,
            preConfirm: function (network) {
                return new Promise(function (resolve, reject) {
                    if (!network) {
                        reject('网络类型为必选项！');
                    } else {
                        resolve();
                    }
                })
            },
            allowOutsideClick: true
        }).then(function (network) {
            $.ajax({
                url: baseUrl + 'Appserver/modifyFirewallNetwork',
                data: {id: rowData.firewallAppId, network: network},
                type: 'POST',
                dataType: 'json',
                complete: function(XMLHttpRequest)
                {
                    var result = XMLHttpRequest.responseJSON || XMLHttpRequest.responseText;
                    if(xhExtra.checkError(result)) return false;
                    window.top.xhTop.showMessage({
                        type: 'success',
                        title: '修改成功！'
                    });
                    $dataGrid.list.datagrid('updateRow', {index: rowIndex, row: result});
                }
            });
        }, function(dismiss) {});
    };
    //启用按钮
    $('#bt_list_enable').click(function(){
        if(xhExtra.buttonDisabled($(this))) return false;
        setStatus(1);
    });
    //禁用按钮
    $('#bt_list_disable').click(function(){
        if(xhExtra.buttonDisabled($(this))) return false;
        setStatus(2);
    });
    //处理启用/禁用
    function setStatus(_status) {
        var id = $dataGrid.getSelectValues('firewallAppId');
        if(!id){
            window.top.xhTop.showMessage({type: 'error', title: '未选择数据！'});
            return false;
        }
        xhExtra.ajaxRequest('Appserver/modifyFirewallStatus', {ids: id, status: _status}, {table: $dataGrid});
    }
});